//
//  DrawTrailerDetailViewController.m
//  JxCarios
//
//  Created by xcq on 15/2/4.
//  Copyright (c) 2015年 xiongchuanqi. All rights reserved.
//

#import "DrawTrailerDetailViewController.h"
#import "RoadsideAssistanceShopDetailTableViewCell.h"
#import "ReplaceDriveDetailPriceCell.h"
#import "ReplaceDriveDetailCallCell.h"
#import "RoadsideAssistanceShopServiceCell.h"
#import "SettlemanetView.h"
#import "PayViewController.h"

static NSString *roadSide = @"RoadsideAssistanceShopDetailTableViewCell";
static NSString *priceStr = @"detailprice";
static NSString *callStr = @"detailcall";
static NSString *cellSte_price_pay = @"RoadsideAssistanceShopServiceCell";


/*
 {
 flag = success;
 seller =     {
        "ERR_NUM" = "<null>";
        "GOOD_NUM" = "<null>";
         ID = 378;
         INTRODUCE = "\U77ee\U5b50\U6c7d\U8f66\U7f8e\U5bb9\U670d\U52a1\U90e8\U4e3b\U8981\U7ecf\U8425\U6c7d\U8f66\U7f8e\U5bb9\U4fdd\U517b\Uff0c\U6c7d\U8f66\U7ef4\U4fee\U7b49\U4e1a\U52a1\U3002";
         "IS_PAY" = yes;
         "SHOP_ADDR" = "\U671d\U9633\U6d32\U4e2d\U8def127\U53f7\U56e2\U7ed3\U8def\U53e3";
         "SHOP_LOGO" = "http://seller.clejw.com/upload/image/201412/b64a8a06-00e4-4bf2-8641-ab42fb37a0a0.jpg";
         "SHOP_NAME" = "\U6b66\U6c49\U5149\U8c37\U77ee\U5b50\U6c7d\U8f66\U7f8e\U5bb9\U670d\U52a1\U90e8";
         TEL = "13879100521,13917777356,null";
         };
 serviceone =     (
         {
         "CHEAP_PRICE" = "360.00";
         "GOOD_NAME" = "\U62d6\U8f66";
         ID = 71;
         "IS_VB" = yes;
         PRICE = "380.00";
         "SEO_DESCRIPTION" = "<null>";
         }
    );
 type = imisellertwo;
 }
 */

@interface DrawTrailerDetailViewController ()<checkActionDelegate>{
    BOOL _is_pay;
    double _checkInteger;
    SettlemanetView *settleView;
    NSMutableArray *seleteShop;
    
}

@end

@implementation DrawTrailerDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initAbout];
    self.tableView.hidden = YES;
}



- (BOOL)netRequest{
    if (self.carDataSource == nil) {
        self.carDataSource = [CarAroundDataSource dataSourceWithRequestor:self];
    }
    if (!_shopId) {
        return NO;
    }
    self.carDataSource.shopId = _shopId;
    self.carDataSource.dic_type = @"pullcar";
    [self.carDataSource getShopTravelAndPullcar:^(id result) {
        __shopDetailDic = [(NSDictionary *)result customObjectForkey:@"seller"];
        _priceArray_one = [(NSDictionary *)result customObjectForkey:@"serviceone"];
        [self initWithDataSource];
    } anfFail:^(id result) {
        
    }];
    return YES;
}

- (void)initWithDataSource {
 
    if ([[__shopDetailDic customObjectForkey:@"IS_PAY"] isEqualToString:@"yes"]) {
        _is_pay = YES;
    }else{
        _is_pay = NO;
    }
    _phoneArray_two = [NSMutableArray arrayWithArray:[[__shopDetailDic customObjectForkey:@"TEL"] componentsSeparatedByString:@","]];
     int i = 0 ;
    BOOL replace = NO;
    for (int a = 0 ;a < _phoneArray_two.count; a ++) {
        NSString *str = [_phoneArray_two objectAtIndex:a];
        if ([str isEqualToString:@"null"]) {
            i = a;
            replace = YES;
        }
    }
    if (replace) {
        [_phoneArray_two removeObjectAtIndex:i];
    }
    self.tableView.hidden = NO;
    [self.tableView reloadData];
}

- (void)initAbout {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    REGISTCELL(@"RoadsideAssistanceShopDetailTableViewCell", roadSide);
    REGISTCELL(@"ReplaceDriveDetailPriceCell", priceStr);
    REGISTCELL(@"ReplaceDriveDetailCallCell", callStr);
//    REGISTCELL(@"RoadsideAssistanceShopServiceCell", cellSte_price_pay);
    
    [self.view addSubview:self.tableView];
    
    self.title = @"商家详情";
    [self showNavBarWithRightTitle:nil andRightAction:nil andLeftAction:nil andCustom:NO];
    
    settleView = [[[NSBundle mainBundle] loadNibNamed:@"SettlemanetView" owner:self options:nil] lastObject];
    settleView.frame = CGRectMake(0,[RextExtends getScreenFrame].size.height - 62 , [RextExtends getScreenFrame].size.width, 62);
    settleView.hidden = YES;
    settleView.tager = self;
    settleView.action = @selector(pushPayViewController);
    [self.view addSubview:settleView];
}

#pragma mark - tableView delegate & dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    switch (section) {
        case 0:
            return 1;
            break;
        case 1:
            return _priceArray_one.count <= 0 ? 0 : _priceArray_one.count;
            break;
        case 2:
            return _phoneArray_two.count <= 0 ? 0 : _phoneArray_two.count;
            break;
        default:
            break;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case 0:{
            RoadsideAssistanceShopDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:roadSide forIndexPath:indexPath];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.info = __shopDetailDic;
            [cell.praiseView removeGestureRecognizer:cell.tap];
            [cell.negativeFeedbackView removeGestureRecognizer:cell.tap_two];
            return cell;
            break;
        }
        case 1:{
            ReplaceDriveDetailPriceCell *price = [tableView dequeueReusableCellWithIdentifier:priceStr forIndexPath:indexPath];
             NSString *str = @"￥";
            price.timeLabel.text = [[_priceArray_one objectAtIndex:indexPath.row] customObjectForkey:@"GOOD_NAME"];
            price.priceLabel.text = [str stringByAppendingString:[[_priceArray_one objectAtIndex:indexPath.row] customObjectForkey:@"CHEAP_PRICE"]];
            price.linePrice.text = [str stringByAppendingString:[[_priceArray_one objectAtIndex:indexPath.row] customObjectForkey:@"PRICE"]];
            return price;
         //   }
            break;
        }
        case 2:{
            ReplaceDriveDetailCallCell *call = [tableView dequeueReusableCellWithIdentifier:callStr forIndexPath:indexPath];
            call.phone.text = [_phoneArray_two objectAtIndex:indexPath.row];
            call.count = indexPath.row;
            return call;
            break;
        }
            
        default:
            break;
    }
    
    return nil;
}
//if(_is_pay){
//                RoadsideAssistanceShopServiceCell *cell = [tableView dequeueReusableCellWithIdentifier:cellSte_price_pay forIndexPath:indexPath];
//                 cell.delegate = self;
//                cell.info = [_priceArray_one objectAtIndex:indexPath.row];
//                return cell;
//   }else{

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    RoadsideAssistanceShopServiceCell *_cell = (RoadsideAssistanceShopServiceCell*)[tableView cellForRowAtIndexPath:indexPath];
//    [_cell checkAction:_cell.checkBtn];
//}

//- (void)checkCallBack:(UITableViewCell *)cell andSeleted:(BOOL)seleted{
//    if (!seleteShop) {
//        seleteShop = [NSMutableArray new];
//    }
//    
//    RoadsideAssistanceShopServiceCell *_cell = (RoadsideAssistanceShopServiceCell*)cell;
//    NSLog(@"cell.info : %@" ,_cell.info);
//    if (seleted) {
//        [seleteShop addObject:_cell.info];
//        [self priceSum:YES price:[_cell.info customObjectForkey:@"CHEAP_PRICE"]];
//        settleView.hidden = NO;
//        //        tableSize = _tableView.contentSize;
//        //        _tableView.contentSize = CGSizeMake(tableSize.width, tableSize.height + 62);
//        
//    }else{
//        [seleteShop removeObject:_cell.info];
//        [self priceSum:NO price:[_cell.info customObjectForkey:@"CHEAP_PRICE"]];
//        if (seleteShop.count == 0) {
//            _checkInteger = 0;
//            settleView.hidden = YES;
//        }
//    }
//}

//- (void)priceSum:(BOOL)bol price:(NSString *)price{
//     if (bol) { //+
//        _checkInteger += [price doubleValue];
//    }else{ //--
//        _checkInteger -= [price doubleValue];
//        
//    }
//     
//    settleView.price = [RMB stringByAppendingString:[NSString stringWithFormat:@"%.2f",_checkInteger]];
//    
//}
//进入支付界面
//- (void)pushPayViewController {
//    PayViewController *pay = [[PayViewController alloc] initWithNibName:@"PayViewController" bundle:nil];
//    pay.buyShopArr = [seleteShop copy];
//    pay.countPrice = _checkInteger;
//    [self.navigationController pushViewController:pay animated:YES];
//}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }else if(section == 1){
        return 20;
    }else{
        return 10;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 0) {
        _detailSize = [Common sizeWithContent:[__shopDetailDic customObjectForkey:@"INTRODUCE"] AndFont:[UIFont systemFontOfSize:13.0f] andSize:[RextExtends getScreenFrame].size andLineBreak:UILineBreakModeWordWrap];
        _detailSize.height += 15;
        return _detailSize.height + 40;
    }
    return 10;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 88;
    }else {
        return 44;
    }
}


- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section == 0) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, _detailSize.height + 40)];
        view.backgroundColor = [UIColor whiteColor];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(2, 2, [RextExtends getScreenFrame].size.width, _detailSize.height)];
        label.backgroundColor = [UIColor redColor];
        if (__shopDetailDic) {
            label.text = [@"    " stringByAppendingString:[__shopDetailDic customObjectForkey:@"INTRODUCE"]] ;
        }
        label.font = [UIFont systemFontOfSize:13.0f];
        //清空背景颜色
        label.backgroundColor = [UIColor clearColor];
        //设置字体颜色为白色
        label.textColor = [UIColor blackColor];
        //文字居中显示
        label.textAlignment = NSTextAlignmentLeft;
        //自动折行设置
        label.lineBreakMode = UILineBreakModeWordWrap;
        label.numberOfLines = 0;
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, label.frame.origin.y + _detailSize.height, [RextExtends getScreenFrame].size.width, 1)];
        lineView.backgroundColor = [UIColor darkGrayColor];
        UILabel *addressLabel = nil;
        CGSize size = [Common sizeWithContent:[__shopDetailDic customObjectForkey:@"SHOP_ADDR"] AndFont:[UIFont systemFontOfSize:13.0f] andSize:CGSizeMake(([RextExtends getScreenFrame].size.width * 0.8 ) - 10 , 40) andLineBreak:UILineBreakModeWordWrap];
        if(size.height > (40 / 2) ){
            addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, lineView.frame.origin.y + 1 + 2, size.width, size.height + 5)];
        }else{
            addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, lineView.frame.origin.y + 1 + 10, size.width, size.height + 5)];
        }
        addressLabel.lineBreakMode = UILineBreakModeWordWrap;
        addressLabel.numberOfLines = 0;
        CGFloat distanceW = [RextExtends getScreenFrame].size.width ;
        UILabel *distanceLabel = [[UILabel alloc] initWithFrame:CGRectMake((distanceW * 0.8) + 20, lineView.frame.origin.y + 1 + 10, distanceW - 15, 20)];
        
        distanceLabel.font = [UIFont systemFontOfSize:11.0f];
        distanceLabel.text = [[NSString stringWithFormat:@"%.1f",[_distance doubleValue] / 1000] stringByAppendingString:@"Km"];
        distanceLabel.textColor = [UIColor grayColor];
        
        UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(distanceW * 0.8, distanceLabel.frame.origin.y + 2, 15, 15)];
        [imageV setImage:[UIImage imageNamed:@"dingwei"]];
        
        
        addressLabel.text = [__shopDetailDic customObjectForkey:@"SHOP_ADDR"];
        addressLabel.textAlignment = NSTextAlignmentLeft;
        addressLabel.font = [UIFont systemFontOfSize:13.0f];
        [view addSubview:distanceLabel];
        [view addSubview:imageV];
        [view addSubview:addressLabel];
        [view addSubview:lineView];
        [view addSubview:label];
        return view;
    }else{
        return nil;
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}


@end
